/**
 * 
 */
package com.onlinemathuracrud.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.onlinemathuracrud.dao.CityDao;
import com.onlinemathuracrud.dao.genericdao.GenericDao;
import com.onlinemathuracrud.model.master.City;

/**
 * @author Amit Kashyap
 * @createdDate Sep 7, 2013
 * @createdTime 1:53:52 AM
 */
@Controller
@RequestMapping(value = "/city")
public class CityCrudController {

	@Autowired
	CityDao cityDao;

	@RequestMapping(value = "/showcities", method = RequestMethod.GET)
	public String showcities(ModelMap model,@ModelAttribute("citydata") City city) {

		model.addAttribute("allCities", cityDao.findAll());
		return "cities";
	}

	@RequestMapping(value = "/editcity/{id}", method = RequestMethod.GET)
	public String editcity(ModelMap model, @PathVariable("id") Long cityId) {
		model.addAttribute("citydata", cityDao.findById(cityId));
		model.addAttribute("allCities", cityDao.findAll());
		return "cities";
	}

	@RequestMapping(value = "/deletecity/{id}", method = RequestMethod.GET)
	public String deletecity(ModelMap model,
			@PathVariable("id") Long templeId,
			@ModelAttribute("citydata") City city) {

		cityDao.delete(cityDao.findById(templeId));
		model.addAttribute("allCities", cityDao.findAll());
		return "cities";
	}

	@RequestMapping(value = "/savecity", method = RequestMethod.POST)
	public String savecity(ModelMap model,City city,HttpServletRequest request) {

		cityDao.saveOrUpdate(city);
		model.addAttribute("citydata", new City());
		model.addAttribute("allCities", cityDao.findAll());
		return "cities";
	}

}
